require.config
	paths:
		jquery: 'lib/jquery-1.7.1.min'
		sammy: 'sammy/sammy-0.7.1.min'
		processing: 'lib/processing-1.3.6-api.min'
		underscore: 'lib/underscore-min'
		jqcouch: 'lib/jquery.couch'
	priority: ['jquery']

require [
	'jquery'
	'sammy'
	'helper'
	'processing'
	'underscore'
	'test'
], ($, S, H) ->

	# Define route actions and page changes.
	index = ->

	# Define a page handler to demonstrate typical behaviour of modules and jQuery plugins.
	test = ->
		$('#main').pl_test(@)
		@change_page 'test', 'input[name="test"]', 'index'
		return

	# Define the main application routes
	app = $.sammy '#main', ->
		@get '#/test', test
		@get '#/'    , index
		@get '#'     , index
		@get ''      , index
		return

	$.sammy '#main', ->

		# All route handlers will use the Sammy.Template plugin
		@use 'Template'

		# Define useful app helpers
		@helpers

			# Define a helper to re-locate to another page.  Re-location will occur
			# if a specified trigger element is clicked. A convention is used to
			# notate the page URL and the ID of its main content container, ie,
			# '#/id' selects the page for the web browser and '#id' selects the DOM
			# container element.
			change_page: (cur_id, trigger, new_id) ->
				$(".page:not(##{cur_id})").hide()
				$cur_id = $("##{cur_id}").show()
				if trigger?
					$cur_id.on 'click', trigger, =>
						@app.setLocation "#/#{new_id}"
						return false
				else
					@redirect "#/#{new_id}"
				return

			# Define a helper to build content from a data object using a template
			# located in a jQuery object.
			content: H.content

		# Apply jQuery plugins to their containers
		$('#main').pl_test(@)
		return

	# Run the app when the DOM is ready.
	$ -> app.run()
	return
